package cn.drenal.capcap.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.drenal.capcap.common.Response;
import cn.drenal.capcap.service.ActivityVoteService;
import org.springframework.web.bind.annotation.*;

/**
 * 活动投票控制器
 */
@RestController
@RequestMapping("/forum/activity-vote")
public class ActivityVoteController {

    private final ActivityVoteService activityVoteService;

    public ActivityVoteController(ActivityVoteService activityVoteService) {
        this.activityVoteService = activityVoteService;
    }

    /**
     * 用户对活动作品进行投票
     *
     * @param activityId 活动ID
     * @param artworkId 作品ID
     * @return 投票结果
     */
    @PostMapping("/vote")
    @SaCheckLogin
    public Response<Boolean> vote(@RequestParam Long activityId, @RequestParam Long artworkId) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = activityVoteService.vote(userId, activityId, artworkId);
        if (result) {
            return Response.success(true);
        } else {
            return Response.error("投票失败");
        }
    }

    /**
     * 获取用户在指定活动中已投票次数
     *
     * @param activityId 活动ID
     * @return 已投票次数
     */
    @GetMapping("/count")
    @SaCheckLogin
    public Response<Integer> getUserVoteCount(@RequestParam Long activityId) {
        Long userId = StpUtil.getLoginIdAsLong();
        int count = activityVoteService.getUserVoteCount(userId, activityId);
        return Response.success(count);
    }

    /**
     * 检查用户是否已经对该作品投过票
     *
     * @param artworkId 作品ID
     * @return 是否已投票
     */
    @GetMapping("/check")
    @SaCheckLogin
    public Response<Boolean> hasVoted(@RequestParam Long artworkId) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean hasVoted = activityVoteService.hasVoted(userId, artworkId);
        return Response.success(hasVoted);
    }

    /**
     * 获取作品在活动中的得票数
     *
     * @param artworkId 作品ID
     * @return 得票数
     */
    @GetMapping("/artwork-count")
    public Response<Integer> getArtworkVoteCount(@RequestParam Long artworkId) {
        int count = activityVoteService.getArtworkVoteCount(artworkId);
        return Response.success(count);
    }
}